package com.yunhai.recipe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.model.entity.fms.FmsMaterialUnitEntity;
import com.yunhai.model.entity.fms.FmsSpecificationConfigEntity;
import com.yunhai.model.entity.fms.FmsSpecificationItemConfigEntity;
import com.yunhai.model.vo.fms.FmsSpecificationConfigQuery;
import com.yunhai.model.vo.fms.FmsSpecificationConfigVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.recipe.mapper.FmsSpecificationConfigMapper;
import com.yunhai.recipe.mapper.FmsSpecificationItemConfigMapper;
import com.yunhai.recipe.service.FmsCodeDictService;
import com.yunhai.recipe.service.FmsSpecificationConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class FmsSpecificationConfigServiceImpl implements FmsSpecificationConfigService {

    @Resource
    private FmsSpecificationConfigMapper fmsSpecificationConfigMapper;
    @Resource
    private FmsSpecificationItemConfigMapper fmsSpecificationItemConfigMapper;
    @Resource
    private FmsCodeDictService fmsCodeDictService;
    /**
     * 获取 FmsSpecificationConfig 分页列表
     */
    @Transactional
    public IPage getFmsSpecificationConfigPagination(PaginationVo paginationVo, FmsSpecificationConfigQuery fmsSpecificationConfigQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<FmsSpecificationConfigEntity> lqs = new LambdaQueryWrapper();
        lqs.eq(FmsSpecificationConfigEntity::getParentId, 0);
        lqs.orderByDesc( FmsSpecificationConfigEntity::getCreateTime  );
        lqs.eq(fmsSpecificationConfigQuery.getEnabled()!=null, FmsSpecificationConfigEntity::getEnabled, fmsSpecificationConfigQuery.getEnabled());
        lqs.likeRight(fmsSpecificationConfigQuery.getNumber() !=null, FmsSpecificationConfigEntity::getNumber, fmsSpecificationConfigQuery.getNumber());
        lqs.likeRight(fmsSpecificationConfigQuery.getName() !=null, FmsSpecificationConfigEntity::getName, fmsSpecificationConfigQuery.getName());
        fmsSpecificationConfigMapper.selectPage(page, lqs);

        List<FmsSpecificationConfigEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }

        //获取子项
        Set<Long> parendIds = record.stream().map( item->item.getId()).collect(Collectors.toSet());
        if( !ObjectUtil.isNotEmptyList(parendIds)){
            return page;
        }


        Set<String> ids = record.stream().map(item->item.getNumber()).collect(Collectors.toSet());
        LambdaQueryWrapper<FmsSpecificationItemConfigEntity> itemLqs = new LambdaQueryWrapper<>();
        itemLqs.in( FmsSpecificationItemConfigEntity::getSpecNumber, ids );
        List<FmsSpecificationItemConfigEntity> itemList = fmsSpecificationItemConfigMapper.selectList(itemLqs);
        if( itemList == null ){
            return  page;
        }

        record.stream().map(item->{
            List<FmsSpecificationItemConfigEntity> children = itemList.stream().filter(row->{
                return row.getSpecNumber().equals(item.getNumber());
            }).collect(Collectors.toList());
            item.setChildren(children);
            return item;
        }).collect(Collectors.toList());

        return page;
    }


    /**
     * 获取 FmsSpecificationConfig 所有数据
     *
    */
    @Transactional
    public List<FmsSpecificationConfigEntity> getFmsSpecificationConfigAllList( FmsSpecificationConfigQuery fmsSpecificationConfigQuery ) throws Exception {

        LambdaQueryWrapper<FmsSpecificationConfigEntity> lqs = new LambdaQueryWrapper();
        List<FmsSpecificationConfigEntity> list = fmsSpecificationConfigMapper.selectList(lqs);
        if( list == null ){
            return  new ArrayList<>();
        }

        Set<String> ids = list.stream().map(item->item.getNumber()).collect(Collectors.toSet());
        LambdaQueryWrapper<FmsSpecificationItemConfigEntity> itemLqs = new LambdaQueryWrapper<>();
        itemLqs.in( FmsSpecificationItemConfigEntity::getSpecNumber, ids );
        List<FmsSpecificationItemConfigEntity> itemList = fmsSpecificationItemConfigMapper.selectList(itemLqs);
        if( itemList == null ){
            return  list;
        }

        list.stream().map(item->{
            List<FmsSpecificationItemConfigEntity> children = itemList.stream().filter(row->{
                return row.getSpecNumber().equals(item.getNumber());
            }).collect(Collectors.toList());
            item.setChildren(children);
            return item;
        }).collect(Collectors.toList());
        return list;
    }


    /**
     * 获取 FmsSpecificationConfig 详情
    */
    @Transactional
    public FmsSpecificationConfigEntity getFmsSpecificationConfigDetail( Long id) throws Exception {

        FmsSpecificationConfigEntity fmsSpecificationConfig = fmsSpecificationConfigMapper.selectById(id);
        return fmsSpecificationConfig;
    }


    /**
     * 新增 FmsSpecificationConfig
     * */
    @Transactional
    public void addFmsSpecificationConfig(FmsSpecificationConfigVo fmsSpecificationConfigVo) throws Exception {

        FmsSpecificationConfigEntity fmsSpecificationConfig = new FmsSpecificationConfigEntity();
        ReflectionCopy.copyProperties( fmsSpecificationConfig, fmsSpecificationConfigVo );
        if( fmsSpecificationConfig.getVersion() == null ){
            fmsSpecificationConfig.setVersion(1);
        }
        fmsSpecificationConfigMapper.insert(fmsSpecificationConfig);

        if( fmsSpecificationConfigVo.getParentId() == null ){
            fmsCodeDictService.updateById(2L, fmsSpecificationConfigVo.getNumber());
        }else {
            fmsCodeDictService.updateById(301L, fmsSpecificationConfigVo.getNumber());
        }
        return;
    }


    /**
     * 更新 FmsSpecificationConfig
     * */
    @Transactional
    public void updateFmsSpecificationConfig( FmsSpecificationConfigVo fmsSpecificationConfigVo) throws Exception {

        if( fmsSpecificationConfigVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        FmsSpecificationConfigEntity fmsSpecificationConfig = new FmsSpecificationConfigEntity();
        ReflectionCopy.copyProperties( fmsSpecificationConfig, fmsSpecificationConfigVo );
        fmsSpecificationConfigMapper.updateById(fmsSpecificationConfig);
        return;
    }

    /**
     * 删除 FmsSpecificationConfig
     * */
    @Transactional
    public void batchRemoveFmsSpecificationConfig( List<Long> ids ) throws Exception {


        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        fmsSpecificationConfigMapper.deleteBatchIds(ids);
    }

}

      